package info.maxhis.bn.sms;

import info.maxhis.bn.BaseActivity;
import info.maxhis.bn.MainActivity;
import info.maxhis.bn.R;
import info.maxhis.service.SMSService;
import info.maxhis.util.SharedData;

import java.util.List;
import java.util.Map;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.res.Resources;
import android.net.Uri;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.KeyEvent;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.MenuItem;
import android.view.MenuItem.OnMenuItemClickListener;
import android.view.View;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;

/**
 * 预设短信列表
 * @author iStar
 *
 */
public class SMSListActivity extends BaseActivity {
	private List<Map<String, String>> data;
	private SMSService ss;
	private SharedData share;
	private Context context;
	private Resources rs;
	private ListView smsList;
	private boolean forSend;//标记是选择发送还是编辑已存短信
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getWindow().requestFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_main_list);//偷个懒，用同一个layout
        context = this;
//        setTitle(R.string.title_sms_list);
        ((TextView)findViewById(R.id.title)).setText(R.string.title_sms_list);
        forSend = getIntent().getBooleanExtra("forSend", false);
        rs = getResources();
        ss = new SMSService(context);
        share = new SharedData(context);
        smsList = (ListView) findViewById(R.id.main_list);
        data = ss.getAllSMS();
		SimpleAdapter adapter = new SimpleAdapter(this, data,
		         R.layout.sms_list_item, new String[] {"smsBody","id"},
		         new int[] {R.id.sms_body,R.id.sms_id});
		smsList.setAdapter(adapter);
		smsList.setTextFilterEnabled(true);
		if (forSend) {
			//发送短信
			smsList.setOnItemClickListener(new AdapterView.OnItemClickListener() {

				@Override
				public void onItemClick(AdapterView<?> parent, View view,
						int position, long id) {
					String phone = getIntent().getStringExtra("phone");
					TextView smsView = (TextView) view.findViewById(R.id.sms_body);
					String smsBody = smsView.getText().toString();
					Intent intent = new Intent(android.content.Intent.ACTION_SENDTO, Uri.parse("smsto:" + phone));
					intent.putExtra("sms_body", smsBody);
					startActivity(intent);
					finish();
				}
			});
		} else {
			smsList.setOnItemLongClickListener(new FriendLongClickListener());
			smsList.setOnCreateContextMenuListener(new View.OnCreateContextMenuListener() {
				
				@Override
				public void onCreateContextMenu(ContextMenu menu, View v,
						ContextMenuInfo menuInfo) {
					menu.setHeaderTitle(R.string.pls_select);
					// 新增
					menu.add(R.string.add).setOnMenuItemClickListener(new OnMenuItemClickListener() {
						
						@Override
						public boolean onMenuItemClick(MenuItem item) {
							Intent intent = new Intent(context, SmsActivity.class);
							intent.putExtra("new", true);
							startActivity(intent);
							finish();
							return false;
						}
					});
					
					//编辑
			        menu.add(R.string.edit).setOnMenuItemClickListener(new OnMenuItemClickListener() {
						
						@Override
						public boolean onMenuItemClick(MenuItem item) {
							Intent editIntent = new Intent(SMSListActivity.this, SmsActivity.class);
							startActivity(editIntent);
							finish();
							return false;
						}
					});
			        //删除
					menu.add(R.string.delete).setOnMenuItemClickListener(new DeleteMenuListener());
				}
			});
		}
    }

    /**
     * 删除菜单监听器
     * @author iStar
     *
     */
    private class DeleteMenuListener implements OnMenuItemClickListener {

		@Override
		public boolean onMenuItemClick(MenuItem item) {
			
			AlertDialog.Builder builder = new AlertDialog.Builder(context);
			String msg = rs.getString(R.string.sms_delete_notice);
			builder.setMessage(msg)
					.setIcon(R.drawable.ask).setTitle(rs.getString(R.string.notice))
					.setPositiveButton(R.string.confern, new DialogInterface.OnClickListener() {
						public void onClick(DialogInterface dialog, int id) {
							ss.removeSMS(share.getSMSId());
							Intent intent = new Intent(context, SMSListActivity.class);
							startActivity(intent);
							finish();
						}
					})
					.setNegativeButton(R.string.cancel, null);
			AlertDialog alert = builder.create();
			alert.show();
			return false;
		}
    	
    }
    /**
     * 长按列表监听器
     * @author iStar
     *
     */
	private class FriendLongClickListener implements OnItemLongClickListener {

		@Override
		public boolean onItemLongClick(AdapterView<?> parent, View view,
				int position, long id) {
			TextView idView = (TextView) view.findViewById(R.id.sms_id);
			String sid = idView.getText().toString();
			share.saveSMSId(sid);
			return false;
		}
    }
/*	
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode ==  KeyEvent.KEYCODE_BACK) {
			Intent intent = new Intent(context, MainActivity.class);
			startActivity(intent);
			finish();
		}
		return super.onKeyDown(keyCode, event);
	}*/
}
